package com.ruoyi.web.controller.base;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.base.BaseTeam;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IBaseTeamService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 班组信息Controller
 *
 * @author ruoyi
 * @date 2023-02-01
 */
@Api(tags = "班组信息api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/team" )
public class BaseTeamController extends BaseController {

    private final IBaseTeamService iBaseTeamService;

    /**
     * 查询班组信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BaseTeam baseTeam)
    {
        startPage();
        LambdaQueryWrapper<BaseTeam> lqw = new LambdaQueryWrapper<BaseTeam>();
        if (StringUtils.isNotBlank(baseTeam.getTeamName())){
            lqw.like(BaseTeam::getTeamName ,baseTeam.getTeamName());
        }
        if (StringUtils.isNotBlank(baseTeam.getTeamCode())){
            lqw.eq(BaseTeam::getTeamCode ,baseTeam.getTeamCode());
        }
        if (StringUtils.isNotBlank(baseTeam.getTeamWorkshop())){
            lqw.eq(BaseTeam::getTeamWorkshop ,baseTeam.getTeamWorkshop());
        }
        if (StringUtils.isNotBlank(baseTeam.getUsers())){
            lqw.eq(BaseTeam::getUsers ,baseTeam.getUsers());
        }
        if (StringUtils.isNotBlank(baseTeam.getStatus())){
            lqw.eq(BaseTeam::getStatus ,baseTeam.getStatus());
        }
        List<BaseTeam> list = iBaseTeamService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出班组信息列表
     */
    @Log(title = "班组信息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(BaseTeam baseTeam) {
        LambdaQueryWrapper<BaseTeam> lqw = new LambdaQueryWrapper<BaseTeam>(baseTeam);
        List<BaseTeam> list = iBaseTeamService.list(lqw);
        ExcelUtil<BaseTeam> util = new ExcelUtil<BaseTeam>(BaseTeam. class);
        return util.exportExcel(list, "team" );
    }

    /**
     * 获取班组信息详细信息
     */
    @GetMapping(value = "/{teamId}" )
    public AjaxResult getInfo(@PathVariable("teamId" ) Long teamId) {
        BaseTeam team = iBaseTeamService.getById(teamId);
        if(StrUtil.isNotEmpty(team.getUsers())){
            List<Integer> listInt = Arrays.stream(team.getUsers().split(","))
                .mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());
            team.setUsersList(listInt);
        }
        return AjaxResult.success(iBaseTeamService.getById(teamId));
    }

    /**
     * 新增班组信息
     */
    @Log(title = "班组信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseTeam baseTeam) {
        baseTeam.setCreateBy(SecurityUtils.getUsername());
        baseTeam.setCreateTime(new Date());
        if(ObjectUtil.isNotNull(baseTeam.getUsersList())){
            baseTeam.setUsers(listToString(baseTeam.getUsersList(),","));
        }
        return toAjax(iBaseTeamService.save(baseTeam) ? 1 : 0);
    }

    public String listToString(List list, String separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i)).append(separator);
        }
        return sb.toString().substring(0,sb.toString().length()-1);
    }

    /**
     * 修改班组信息
     */
    @Log(title = "班组信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseTeam baseTeam) {
        baseTeam.setUpdateBy(SecurityUtils.getUsername());
        baseTeam.setUpdateTime(new Date());
        if(ObjectUtil.isNotNull(baseTeam.getUsersList())){
            baseTeam.setUsers(listToString(baseTeam.getUsersList(),","));
        }
        return toAjax(iBaseTeamService.updateById(baseTeam) ? 1 : 0);
    }

    /**
     * 删除班组信息
     */
    @Log(title = "班组信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{teamIds}" )
    public AjaxResult remove(@PathVariable Long[] teamIds) {
        return toAjax(iBaseTeamService.removeByIds(Arrays.asList(teamIds)) ? 1 : 0);
    }
}
